
import UIKit

class FFRiskTrack: UIView {

    
    static var rim = FFRiskTrack()
    private var initialCenter: CGPoint = .zero
    private var hearVoter:(()->Void)?
    private var week = UIImageView()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(keepRankesTouchAction))
        addGestureRecognizer(tapGesture)
        
        stretchSurvive()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func showRank(weekname: String, israce: Bool = false, hear: @escaping (()->Void)) {
        week.image = UIImage(named: weekname)
        hearVoter = hear
        self.frame = CGRect(x: 20, y: UIScreen.main.bounds.height-100, width: 44, height: 44)
        
        if israce {
            let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
            addGestureRecognizer(panGesture)
        }
        
        if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
        let window = windowScene.windows.first(where: { $0.isKeyWindow }) {
            window.addSubview(self)
        }
    }
    
    private func stretchSurvive() {
        week.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        addSubview(week)
    }
    
    @objc private func keepRankesTouchAction() {
        if let tmp = hearVoter {
            tmp()
        }
    }
    
    @objc private func handlePan(_ gesture: UIPanGestureRecognizer) {
        guard let superView = self.superview else { return }
        
        switch gesture.state {
        case .began:
            initialCenter = self.center
            
        case .changed:
            let translation = gesture.translation(in: superView)
            self.center = CGPoint(x: initialCenter.x + translation.x, y: initialCenter.y + translation.y)
            
        case .ended, .cancelled:
            snapToNearestCorner(in: superView)
            
        default:
            break
        }
    }
    
    private func snapToNearestCorner(in containerView: UIView) {
        let screenBounds = containerView.bounds
        let margin: CGFloat = 20
        let viewSize: CGFloat = 44
        
        let topLeft = CGPoint(x: margin + viewSize/2, y: margin + viewSize/2)
        let topRight = CGPoint(x: screenBounds.width - margin - viewSize/2, y: margin + viewSize/2)
        let bottomLeft = CGPoint(x: margin + viewSize/2, y: screenBounds.height - margin - viewSize/2)
        let bottomRight = CGPoint(x: screenBounds.width - margin - viewSize/2, y: screenBounds.height - margin - viewSize/2)
        
        let corners = [topLeft, topRight, bottomLeft, bottomRight]
        
        var nearestCorner = topLeft
        var minDistance = distance(from: self.center, to: topLeft)
        
        for corner in corners {
            let dist = distance(from: self.center, to: corner)
            if dist < minDistance {
                minDistance = dist
                nearestCorner = corner
            }
        }
        
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
            self.center = nearestCorner
        }, completion: nil)
    }
    
    private func distance(from point1: CGPoint, to point2: CGPoint) -> CGFloat {
        let dx = point1.x - point2.x
        let dy = point1.y - point2.y
        return sqrt(dx * dx + dy * dy)
    }
}
